package com.work.leecode.dp.middle;

/**
 * 最长回文子串---5
 */
public class Lc5 {
    //动态规划
    public String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }

        int maxLen = 1;
        //记录开始的索引
        int begin = 0;

        // dp[i][j] 表示s[i..j]是否是回文串
        boolean[][] dp = new boolean[len][len];

        //初始化：所有中间长度为1的子串都是回文串
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }

        char[] charArray = s.toCharArray();

        //考虑子串长度从2开始
        for (int L = 2; L <= len; L++) {
            for (int i = 0; i < len; i++) {
                //子串的右边界
                int j = L + i - 1;
                if (j > len) {
                    break;
                }

                if (charArray[i] != charArray[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }
}
